
#include <BPainter>
#include <BPixmap>
#include <BGlyph>
#include "ImageLayer.h"

ImageLayer::ImageLayer(BWidget* parent) : BWidget(parent)
{

}
ImageLayer::ImageLayer(BImage* image, BWidget* parent) : BWidget(parent)
{
	if (image)
	{
		BString name = image->fileName().name();
		name -= name.dext();
		this->setTitle(name);
		this->setImage(image);
		this->setSize(image->size());
	}
}
ImageLayer::ImageLayer(const ImageLayer& other)
{
	this->copy(&other);
}
ImageLayer::~ImageLayer()
{

}

void ImageLayer::copy(const BWidget* other)
{
	BWidget::copy(other);
	if (const ImageLayer* layer = dynamic_cast<const ImageLayer*>(other))
	{
		_image = layer->_image->clone();
	}
}
BWidget* ImageLayer::clone() const
{
	return new ImageLayer(*this);
}

void ImageLayer::setImage(BImage* image)
{
	if (_image != image)
	{
		if (_image)
			disconnect(_image);
		_image = image;
		if (_image)
			connect(_image, Signal_Dirty, &ImageLayer::slotImageDirty);
		this->fresh();
	}
	this->setSize(_image->size());
}
BImage* ImageLayer::image() const
{
	return _image;
}

void ImageLayer::slotImageDirty(BImage* image)
{
	this->setSize(image->size());
	this->fresh();
}

void ImageLayer::resizeEvent(const BEvent& event)
{
	if (BGlyph* graphic = _image.as<BGlyph>())
	{
		graphic->setSize(event.value());
	}
	BWidget::resizeEvent(event);
}
void ImageLayer::freshEvent(const BEvent& event)
{
	BWidget::freshEvent(event);
}
void ImageLayer::paintEvent(const BEvent& event)
{
	BPainter painter(this);
	painter.setColor(100, 0, 100);
	painter.drawImage(0, 0, this->width(), this->height(), _image);
}
